import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import sun.misc.REException;

import javax.swing.text.html.HTMLDocument;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by yangxianming on 2015-07-29.
 */

//class Swagger{
//    public String name;
//    public int age;
//
//}

public class TestJson {
    public static void main(String[] args) {
//        String myJson = "{\"result\":1,\"data\":{\"isTermination\":false,\"isEnd\":false,\"restTime\":604800000,\"questionDetail\":{\"id\":168430,\"code\":\"168430\",\"surveyId\":75248,\"content\":\"选择一个页面作为问答任务推广页面。\",\"questionImage\":\"\",\"questionImageList\":[\"\"],\"sortedNum\":1,\"questionType\":\"RADIO\",\"type\":1,\"optionCount\":0,\"allowWord\":false,\"optionStr\":\"{\\\"canSubmit\\\":false,\\\"mandatory\\\":true,\\\"options\\\":[{\\\"id\\\":1164,\\\"text\\\":\\\"ID:1164 不朽之王禁锢地牢笼\\\"},{\\\"id\\\":1163,\\\"text\\\":\\\"ID:1163 不朽之王禁锢地牢笼\\\"},{\\\"id\\\":1162,\\\"text\\\":\\\"ID:1162 不朽之王禁锢地牢笼\\\"},{\\\"id\\\":1161,\\\"text\\\":\\\"ID:1161 jdjdjdjd\\\"}]}\",\"show\":false,\"flagReference\":false,\"optionScheme\":{\"mutexes\":[],\"isRandomSort\":false,\"options\":[{\"id\":1161,\"code\":\"1161\",\"text\":\"ID:1161 jdjdjdjd\",\"imageList\":[]},{\"id\":1162,\"code\":\"1162\",\"text\":\"ID:1162 不朽之王禁锢地牢笼\",\"imageList\":[]},{\"id\":1163,\"code\":\"1163\",\"text\":\"ID:1163 不朽之王禁锢地牢笼\",\"imageList\":[]},{\"id\":1164,\"code\":\"1164\",\"text\":\"ID:1164 不朽之王禁锢地牢笼\",\"imageList\":[]}],\"mandatory\":true,\"canSubmit\":false,\"showType\":0},\"questionDescStatus\":false,\"videoDetail\":{\"skipFlag\":false,\"pauseFlag\":false},\"hasAllowWord\":false,\"containLogic\":false,\"currentAnsweredQuestion\":false},\"questionCount\":5,\"isChooseAddress\":false,\"surveyAwardType\":6,\"taskType\":0,\"sunnySwitch\":false,\"refuse\":false,\"showQuestionIndex\":false,\"isSucceed\":true,\"prevDisabled\":false,\"payable\":false,\"attributes\":{\"id\":815},\"containLogic\":false}}";
        String path = System.getProperty("user.dir") + "/data/";
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader((new File(path+"swagger-ws.json"))));
            StringBuffer str = new StringBuffer();
            String s = "";
            while ((s = bufferedReader.readLine()) != null){
                s = s.replaceAll("\\s","");
                str = str.append(s);
            }
            s = str.toString();
            try {
//                JSONObject object =  JSON.parseObject(s);
//                JSONObject find = object.getJSONObject("paths").getJSONObject("/vi/surveysvc/category");
//                System.out.println(find.toString());

                JSONObject object =  JSON.parseObject(s);
                String jsonpath = "paths";
                String[] jsonPathArr = jsonpath.split("\\|");
//                System.out.println(object.getJSONObject("data").toString());

                for (int i=0; i<jsonPathArr.length-1; i++){
                    String onePath = jsonPathArr[i];
                    if (object.containsValue(new JSONArray())){
                        int index = 0;
                        if (onePath.contains("[") && onePath.contains("]")){
                            index = Integer.valueOf(onePath.substring(onePath.indexOf("[")+1, onePath.lastIndexOf("]")));
                            onePath = onePath.substring(0, onePath.lastIndexOf("["));
                        }
                        JSONArray objectsArr = object.getJSONArray(onePath);
                        object = objectsArr.getJSONObject(index);
                        continue;
                    }
                    object = object.getJSONObject(onePath);
//                    System.out.println(object.toString());
                }

//                最后一个路径直接转换成map然后把key对应的value直接转换成string输出
                String lastPath = jsonPathArr[jsonPathArr.length - 1];
                for (Map.Entry<String, Object> me:object.entrySet()){
                    if (me.getKey().equals(lastPath)){
                        System.out.println(me.getValue().toString());
                        break;
                    }
                }


            } catch (Exception e) {
                e.printStackTrace();
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
